package com.neusoft.databus.common.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractModel;
import com.neusoft.databus.common.enums.MetricValueCategory;
import com.neusoft.databus.common.enums.MetricValueType;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 监控指标项Model
 *
 * @author sunchf
 * @date 2018年12月20日 下午2:36:58
 */
@Getter
@Setter
@Entity
@ToString
//@TypeDefs({ @TypeDef(name = "StringJsonObject", typeClass = StringJsonUserType.class) })
public class Metric extends AbstractModel {

    private static final long serialVersionUID = 5672603493339511039L;

    /**
     * 指标名称
     */
    @NotBlank(message = "指标名称不能为空")
    @Length(max = 255, message = "指标名称不能超过{max}位")
    @Column(length = 255, nullable = false)
    private String name;

    /**
     * 指标编码
     */
    @NotBlank(message = "指标编码不能为空")
    @Length(max = 255, message = "指标编码不能超过{max}位")
    @Column(length = 255, nullable = false, unique = true)
    private String code;

    /**
     * 指标变量名
     */
    @NotBlank(message = "指标变量名不能为空")
    @Length(max = 255, message = "指标变量名不能超过{max}位")
    @Column(length = 255, nullable = false)
    private String variable;

    /**
     * 指标值数据类型
     */
    @Enumerated(EnumType.STRING)
    @NotNull(message = "指标值数据类型不能为空")
    @Column(nullable = false)
    private MetricValueType valueType;

    /**
     * 指标值截取规则配置
     */
    // @Type(type = "StringJsonObject")
    @Length(max = 255, message = "指标值截取规则配置不能超过{max}位")
    @Column(length = 255)
    private String rule;

    /**
     * 为获取指标值而发送的特定协议命令 <br>
     * 如：Modbus-RTU：0300000002 <br>
     * 如：Modbus-ASCII：000000 <br>
     * 如：Modbus-TCP：？？？？ <br>
     */
    @NotBlank(message = "指令不能为空")
    @Lob
    @Column(nullable = false, columnDefinition = "Text")
    private String command;

    /**
     * 指标组。关系在MetricGroup中维护
     */
    @ManyToMany
    @JoinTable(name = "ref_metric_group", joinColumns = {
            @JoinColumn(name = "metric_id", referencedColumnName = "id") }, inverseJoinColumns = {
                    @JoinColumn(name = "metric_group_id", referencedColumnName = "id") })
    private List<MetricGroup> groups = new ArrayList<>();

    /** 指标单位 */
    //@NotBlank(message = "指标单位不能为空")
    @Length(max = 255, message = "指标单位不能超过{max}位")
    @Column(length = 255)
    private String unit;

    /**
     * 指标值数据类型
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = true)
    private MetricValueCategory valueCategory = MetricValueCategory.OTHER;

    /** 注释 */
    @Length(max = 1023, message = "注释不能超过{max}位")
    @Column(length = 1023, nullable = true)
    private String comment;

}
